package com.dmz.workflow.util;

import com.dmz.workflow.constant.ExpressionConstants;
import com.dmz.workflow.constant.ProcessDefinitionConstants;
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.*;

import java.util.*;


/**
 * 画图功能抽取
 * 关联表单时后端需在表单key后加 .form
 */
public class ProcessModelFactory {
    /**
     * 开始事件
     */
    public StartEvent createStartEvent(String id, String name, String formKey, List<SequenceFlow> outgoingFlows) {
        StartEvent startEvent = new StartEvent();
        startEvent.setId(id);
        startEvent.setName(name);
        startEvent.setFormKey(formKey);
        startEvent.setOutgoingFlows(outgoingFlows);
        startEvent.setSameDeployment(true);
        startEvent.setInterrupting(true);
        startEvent.setValidateFormFields("true");
        return startEvent;
    }

    /**
     * 第一个发起人节点
     */
    public UserTask createInitiator(String formKey, List<SequenceFlow> outgoingFlows) {
        UserTask userTask = new UserTask();
        userTask.setId(ProcessDefinitionConstants.START_NODE_NAME);
        userTask.setName("发起人");
        userTask.setFormKey(formKey);
        userTask.setOutgoingFlows(outgoingFlows);
        ExtensionElement extensionElement = new ExtensionElement();
        extensionElement.setName("initiator-can-complete");
        extensionElement.setNamespacePrefix("modeler");
        extensionElement.setNamespace("http://flowable.org/modeler");
        extensionElement.setElementText("true");
        Map<String, List<ExtensionElement>> extensionElements = new HashMap<>();
        List<ExtensionElement> list = new ArrayList<>();
        list.add(extensionElement);
        extensionElements.put("initiator-can-complete", list);
        userTask.setExtensionElements(extensionElements);
        return userTask;
    }

    /**
     * 结束事件
     */
    public EndEvent createEndEvent(String id, String name) {
        EndEvent endEvent = new EndEvent();
        endEvent.setId(id);
        endEvent.setName(name);
        return endEvent;
    }

    /**
     * fieldName  ： 是人 还是角色 还是部门
     */
    public ServiceTask createServiceTask(String id, String name, List<SequenceFlow> outgoingFlows) {
        ServiceTask serviceTask = new ServiceTask();
        serviceTask.setId(id);
        serviceTask.setName(name);
        serviceTask.setImplementation("${copyService}");
        serviceTask.setImplementationType("delegateExpression");
        serviceTask.setOutgoingFlows(outgoingFlows);
        return serviceTask;
    }

    /**
     * 会签任务节点
     */
    public UserTask createCounterSignUserTask(String id, String name, String formKey, List<SequenceFlow> outgoingFlows, Boolean isSerial) {
        return createUserTask(id, name, formKey, outgoingFlows, isSerial, false);
    }

    /**
     * 或签任务节点
     */
    public UserTask createOrSignUserTask(String id, String name, String formKey, List<SequenceFlow> outgoingFlows, Boolean isSerial) {
        return createUserTask(id, name, formKey, outgoingFlows, isSerial, true);
    }

    /**
     * 顺序流线 带节点名称的
     */
    public SequenceFlow createSequenceFlow(String from, String to, String name, String conditionExpression) {
        SequenceFlow sequenceFlow = new SequenceFlow();
        String id = "SF" + UUID.randomUUID();
        sequenceFlow.setId(id);
        sequenceFlow.setName(name);
        if (StringUtils.isNotEmpty(from)) {
            sequenceFlow.setSourceRef(from);
        }
        if (StringUtils.isNotEmpty(to)) {
            sequenceFlow.setTargetRef(to);
        }
        if (StringUtils.isNotEmpty(conditionExpression)) {
            sequenceFlow.setConditionExpression(conditionExpression);
        }
        return sequenceFlow;
    }

    /**
     * 排他网关
     */
    public ExclusiveGateway createExclusiveGateway(String id, String name, List<SequenceFlow> list) {
        ExclusiveGateway exclusiveGateway = new ExclusiveGateway();
        exclusiveGateway.setId(id);
        exclusiveGateway.setName(name);
        exclusiveGateway.setOutgoingFlows(list);
        return exclusiveGateway;
    }

    /**
     * 设置数据对象属性 组件
     */
    public BooleanDataObject getDataObject() {
        BooleanDataObject dataObject = new BooleanDataObject();
        dataObject.setId("businessKey");
        dataObject.setName("businessKey");
        dataObject.setValue("true");
        ItemDefinition itemSubjectRef = new ItemDefinition();
        itemSubjectRef.setStructureRef("xsd:boolean");
        dataObject.setItemSubjectRef(itemSubjectRef);
        return dataObject;
    }

    /**
     * 或签任务节点
     */
    public UserTask createUserTask(String id, String name, String formKey, List<SequenceFlow> outgoingFlows, Boolean isSerial, Boolean isOrSign) {
        UserTask userTask = new UserTask();
        userTask.setId(id);
        userTask.setName(name);
        userTask.setAssignee("${" + id + "}");
        userTask.setFormKey(formKey);
        MultiInstanceLoopCharacteristics multiInstanceLoopCharacteristics = new MultiInstanceLoopCharacteristics();
        // 设置为串并行（false为并行执行，true为串行执行）
        multiInstanceLoopCharacteristics.setSequential(isSerial);
        // 设置会签或签的完成条件
        if (isOrSign) {
            multiInstanceLoopCharacteristics.setCompletionCondition(ExpressionConstants.OR_SIGN_EXPRESSION);
        } else {
            multiInstanceLoopCharacteristics.setCompletionCondition(ExpressionConstants.COUNTER_SIGN_EXPRESSION);
        }
        // 设置审批人集合参数 注意参数名不可重复 预防混乱数据
        multiInstanceLoopCharacteristics.setInputDataItem(id + "List");
        // 元素变量多实例,一般和设置的assignee变量是对应的
        multiInstanceLoopCharacteristics.setElementVariable(id);
        userTask.setLoopCharacteristics(multiInstanceLoopCharacteristics);
        userTask.setOutgoingFlows(outgoingFlows);
        return userTask;
    }
}

